package com.esri.core.geometry;

import com.esri.core.geometry.SpatialReference;
import java.io.IOException;
import java.io.StringWriter;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerator;

/* loaded from: classes.dex */
class OperatorExportToJsonCursor extends JsonCursor {
    private static JsonFactory f = new JsonFactory();
    GeometryCursor a;
    int b = -1;
    int c;
    int d;
    String e;

    public OperatorExportToJsonCursor(SpatialReference spatialReference, GeometryCursor geometryCursor) {
        this.c = -1;
        this.d = -1;
        this.e = null;
        if (geometryCursor == null) {
            throw new IllegalArgumentException();
        }
        if (spatialReference != null && spatialReference.getCoordinateSystemType() != SpatialReference.Type.LOCAL) {
            this.c = spatialReference.getOldID();
            this.e = spatialReference.getText();
            this.d = spatialReference.getLatestID();
        }
        this.a = geometryCursor;
    }

    @Override // com.esri.core.geometry.JsonCursor
    public int getID() {
        return this.b;
    }

    @Override // com.esri.core.geometry.JsonCursor
    public String next() {
        Geometry next = this.a.next();
        if (next != null) {
            this.b = this.a.getGeometryID();
            return a(next);
        }
        return null;
    }

    private String a(Geometry geometry) {
        StringWriter stringWriter = new StringWriter();
        try {
            JsonGenerator createJsonGenerator = f.createJsonGenerator(stringWriter);
            int value = geometry.getType().value();
            if (value == 513) {
                a(createJsonGenerator, (Point) geometry);
            } else if (value == 3077) {
                a(createJsonGenerator, (Envelope) geometry);
            } else if (value == 8710) {
                a(createJsonGenerator, (MultiPoint) geometry);
            } else if (value == 25607) {
                a(createJsonGenerator, (Polyline) geometry);
            } else if (value == 27656) {
                a(createJsonGenerator, (Polygon) geometry);
            } else {
                throw new RuntimeException("not implemented for this geometry type");
            }
            return stringWriter.getBuffer().toString();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    private void a(JsonGenerator jsonGenerator, Polygon polygon) throws JsonGenerationException, IOException {
        a(jsonGenerator, polygon, "rings");
    }

    private void a(JsonGenerator jsonGenerator, Polyline polyline) throws JsonGenerationException, IOException {
        a(jsonGenerator, polyline, "paths");
    }

    private void a(JsonGenerator jsonGenerator, MultiPath multiPath, String str) throws JsonGenerationException, IOException {
        boolean e = multiPath.e(1);
        boolean e2 = multiPath.e(2);
        jsonGenerator.writeStartObject();
        if (e) {
            jsonGenerator.writeFieldName("hasZ");
            jsonGenerator.writeBoolean(true);
        }
        if (e2) {
            jsonGenerator.writeFieldName("hasM");
            jsonGenerator.writeBoolean(true);
        }
        jsonGenerator.writeFieldName(str);
        jsonGenerator.writeStartArray();
        if (!multiPath.isEmpty()) {
            int pathCount = multiPath.getPathCount();
            MultiPathImpl multiPathImpl = (MultiPathImpl) multiPath._getImpl();
            AttributeStreamOfDbl attributeStreamOfDbl = e ? (AttributeStreamOfDbl) multiPathImpl.getAttributeStreamRef(1) : null;
            AttributeStreamOfDbl attributeStreamOfDbl2 = e2 ? (AttributeStreamOfDbl) multiPathImpl.getAttributeStreamRef(2) : null;
            boolean z = multiPath instanceof Polygon;
            Point2D point2D = new Point2D();
            for (int i = 0; i < pathCount; i++) {
                jsonGenerator.writeStartArray();
                int pathStart = multiPath.getPathStart(i);
                int pathSize = multiPath.getPathSize(i);
                for (int i2 = pathStart; i2 < pathStart + pathSize; i2++) {
                    multiPath.getXY(i2, point2D);
                    jsonGenerator.writeStartArray();
                    a(point2D.x, jsonGenerator);
                    a(point2D.y, jsonGenerator);
                    if (e) {
                        a(attributeStreamOfDbl.get(i2), jsonGenerator);
                    }
                    if (e2) {
                        a(attributeStreamOfDbl2.get(i2), jsonGenerator);
                    }
                    jsonGenerator.writeEndArray();
                }
                if (z) {
                    multiPath.getXY(pathStart, point2D);
                    jsonGenerator.writeStartArray();
                    jsonGenerator.writeNumber(point2D.x);
                    jsonGenerator.writeNumber(point2D.y);
                    if (e) {
                        a(attributeStreamOfDbl.get(pathStart), jsonGenerator);
                    }
                    if (e2) {
                        a(attributeStreamOfDbl2.get(pathStart), jsonGenerator);
                    }
                    jsonGenerator.writeEndArray();
                }
                jsonGenerator.writeEndArray();
            }
        }
        jsonGenerator.writeEndArray();
        a(jsonGenerator);
        jsonGenerator.writeEndObject();
        jsonGenerator.close();
    }

    private void a(JsonGenerator jsonGenerator, MultiPoint multiPoint) throws JsonGenerationException, IOException {
        boolean e = multiPoint.e(1);
        boolean e2 = multiPoint.e(2);
        jsonGenerator.writeStartObject();
        if (e) {
            jsonGenerator.writeFieldName("hasZ");
            jsonGenerator.writeBoolean(true);
        }
        if (e2) {
            jsonGenerator.writeFieldName("hasM");
            jsonGenerator.writeBoolean(true);
        }
        jsonGenerator.writeFieldName("points");
        jsonGenerator.writeStartArray();
        if (!multiPoint.isEmpty()) {
            MultiPointImpl multiPointImpl = (MultiPointImpl) multiPoint._getImpl();
            AttributeStreamOfDbl attributeStreamOfDbl = e ? (AttributeStreamOfDbl) multiPointImpl.getAttributeStreamRef(1) : null;
            AttributeStreamOfDbl attributeStreamOfDbl2 = e2 ? (AttributeStreamOfDbl) multiPointImpl.getAttributeStreamRef(2) : null;
            Point2D point2D = new Point2D();
            int pointCount = multiPoint.getPointCount();
            for (int i = 0; i < pointCount; i++) {
                multiPoint.getXY(i, point2D);
                jsonGenerator.writeStartArray();
                a(point2D.x, jsonGenerator);
                a(point2D.y, jsonGenerator);
                if (e) {
                    a(attributeStreamOfDbl.get(i), jsonGenerator);
                }
                if (e2) {
                    a(attributeStreamOfDbl2.get(i), jsonGenerator);
                }
                jsonGenerator.writeEndArray();
            }
        }
        jsonGenerator.writeEndArray();
        a(jsonGenerator);
        jsonGenerator.writeEndObject();
        jsonGenerator.close();
    }

    private void a(JsonGenerator jsonGenerator, Point point) throws JsonGenerationException, IOException {
        boolean e = point.e(1);
        boolean e2 = point.e(2);
        jsonGenerator.writeStartObject();
        if (point.isEmpty()) {
            jsonGenerator.writeFieldName("x");
            jsonGenerator.writeNull();
            jsonGenerator.writeFieldName("y");
            jsonGenerator.writeNull();
            if (e) {
                jsonGenerator.writeFieldName("z");
                jsonGenerator.writeNull();
            }
            if (e2) {
                jsonGenerator.writeFieldName("m");
                jsonGenerator.writeNull();
            }
        } else {
            jsonGenerator.writeFieldName("x");
            a(point.getX(), jsonGenerator);
            jsonGenerator.writeFieldName("y");
            a(point.getY(), jsonGenerator);
            if (e) {
                jsonGenerator.writeFieldName("z");
                a(point.getZ(), jsonGenerator);
            }
            if (e2) {
                jsonGenerator.writeFieldName("m");
                a(point.getM(), jsonGenerator);
            }
        }
        a(jsonGenerator);
        jsonGenerator.writeEndObject();
        jsonGenerator.close();
    }

    private void a(JsonGenerator jsonGenerator, Envelope envelope) throws JsonGenerationException, IOException {
        boolean e = envelope.e(1);
        boolean e2 = envelope.e(2);
        jsonGenerator.writeStartObject();
        if (envelope.isEmpty()) {
            jsonGenerator.writeFieldName("xmin");
            jsonGenerator.writeNull();
            jsonGenerator.writeFieldName("ymin");
            jsonGenerator.writeNull();
            jsonGenerator.writeFieldName("xmax");
            jsonGenerator.writeNull();
            jsonGenerator.writeFieldName("ymax");
            jsonGenerator.writeNull();
            if (e) {
                jsonGenerator.writeFieldName("zmin");
                jsonGenerator.writeNull();
                jsonGenerator.writeFieldName("zmax");
                jsonGenerator.writeNull();
            }
            if (e2) {
                jsonGenerator.writeFieldName("mmin");
                jsonGenerator.writeNull();
                jsonGenerator.writeFieldName("mmax");
                jsonGenerator.writeNull();
            }
        } else {
            jsonGenerator.writeFieldName("xmin");
            a(envelope.getXMin(), jsonGenerator);
            jsonGenerator.writeFieldName("ymin");
            a(envelope.getYMin(), jsonGenerator);
            jsonGenerator.writeFieldName("xmax");
            a(envelope.getXMax(), jsonGenerator);
            jsonGenerator.writeFieldName("ymax");
            a(envelope.getYMax(), jsonGenerator);
            if (e) {
                Envelope1D queryInterval = envelope.queryInterval(1, 0);
                jsonGenerator.writeFieldName("zmin");
                a(queryInterval.vmin, jsonGenerator);
                jsonGenerator.writeFieldName("zmax");
                a(queryInterval.vmax, jsonGenerator);
            }
            if (e2) {
                Envelope1D queryInterval2 = envelope.queryInterval(2, 0);
                jsonGenerator.writeFieldName("mmin");
                a(queryInterval2.vmin, jsonGenerator);
                jsonGenerator.writeFieldName("mmax");
                a(queryInterval2.vmax, jsonGenerator);
            }
        }
        a(jsonGenerator);
        jsonGenerator.writeEndObject();
        jsonGenerator.close();
    }

    private void a(double d, JsonGenerator jsonGenerator) throws IOException, JsonGenerationException {
        if (NumberUtils.isNaN(d)) {
            jsonGenerator.writeNull();
        } else {
            jsonGenerator.writeNumber(d);
        }
    }

    private void a(JsonGenerator jsonGenerator) throws IOException, JsonGenerationException {
        if (this.c > 0) {
            jsonGenerator.writeFieldName("spatialReference");
            jsonGenerator.writeStartObject();
            jsonGenerator.writeFieldName("wkid");
            jsonGenerator.writeNumber(this.c);
            int i = this.d;
            if (i > 0 && i != this.c) {
                jsonGenerator.writeFieldName("latestWkid");
                jsonGenerator.writeNumber(this.d);
            }
            jsonGenerator.writeEndObject();
        } else if (this.e != null) {
            jsonGenerator.writeFieldName("spatialReference");
            jsonGenerator.writeStartObject();
            jsonGenerator.writeFieldName("wkt");
            jsonGenerator.writeString(this.e);
            jsonGenerator.writeEndObject();
        }
    }
}
